package com.invengo.testrfid.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;

import com.invengo.testrfid.R;
import com.invengo.testrfid.app.entity.ReadTagEntity;
import com.invengo.testrfid.app.view.InitTagView;
import com.invengo.testrfid.app.view.InputCageView;
import com.invengo.testrfid.app.view.PreOrNextView;
import com.invengo.testrfid.di.component.DaggerAddCageNewComponent;
import com.invengo.testrfid.di.module.AddCageNewModule;
import com.invengo.testrfid.mvp.contract.AddCageNewContract;
import com.invengo.testrfid.mvp.presenter.AddCageNewPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;


import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class AddCageNewActivity extends BaseActivity<AddCageNewPresenter>
        implements AddCageNewContract.View {

    @BindView(R.id.input_view)
    InputCageView inputCageView;

    @BindView(R.id.init_one)
    InitTagView initTagViewOne;

    @BindView(R.id.init_two)
    InitTagView initTagViewTwo;

    // 当前初始化标签界面
    private InitTagView curTagView;

    // 当前笼车编号
    private String curCage;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerAddCageNewComponent
                .builder()
                .appComponent(appComponent)
                .addCageNewModule(new AddCageNewModule(this)) //请将AddCageNewModule()第一个首字母改为小写
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_add_cage_new;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        setTitle("步骤1：输入集装箱编号");
        mPresenter.connect();


        inputCageView.setPreOrNextClick(new PreOrNextView.PreOrNextCallback() {

            @Override
            public void onNextClick() {

                String b = inputCageView.getText().trim().replace(" ","");
                curCage = b;
                mPresenter.onInputViewNextClick(AddCageNewActivity.this, curCage);
            }

            @Override
            public void onPreClick() {


            }
        });
        initTagViewOne.addPreOrNextCallbace(new PreOrNextView.PreOrNextCallback() {

            @Override
            public void onPreClick() {

                showFirstView();
            }

            @Override
            public void onNextClick() {

                showThreeView();
            }
        });
        initTagViewTwo.addPreOrNextCallbace(new PreOrNextView.PreOrNextCallback() {

            @Override
            public void onPreClick() {

                showSecondView();
            }

            @Override
            public void onNextClick() {

                mPresenter.saveData(curCage);
            }
        });
        initTagViewTwo.initNextTitle("提交数据");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode != 2) {
            return super.onKeyDown(keyCode, event);
        } else {

            if (isReadTag) {
                if (readTagPosition == 1) {
                    mPresenter.readTag(curCage, true);
                } else {
                    mPresenter.readTag(curCage, false);
                }
            }
            return true;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

        mPresenter.disConnect();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    private boolean isReadTag = false;
    private ReadTagEntity readTagEntityOne;
    private ReadTagEntity readTagEntityTwo;
    private int readTagPosition = 0;

    /**
     * 显示第一个界面
     */
    public void showFirstView() {

        setTitle("步骤1：输入集装箱编号");
        isReadTag = false;
        inputCageView.setVisibility(View.VISIBLE);
        initTagViewOne.setVisibility(View.GONE);
        initTagViewTwo.setVisibility(View.GONE);
    }

    @Override
    public void clearAllData() {

        readTagEntityOne = null;
        readTagEntityTwo = null;
        inputCageView.clearText();
    }

    @Override
    public void showSecondView() {

        setTitle("步骤2：初始化第1张标签");
        readTagPosition = 1;
        inputCageView.setVisibility(View.GONE);
        initTagViewTwo.setVisibility(View.GONE);
        initTagViewOne.setVisibility(View.VISIBLE);
        curTagView = initTagViewOne;
        isReadTag = true;
        if (readTagEntityOne == null)
            curTagView.initView("", "", curCage);
        else
            curTagView.initView(readTagEntityOne.getTid(), readTagEntityOne.getEpc(), curCage);
    }

    /**
     * 显示第三个界面
     */
    private void showThreeView() {

        setTitle("步骤3： 初始化第2张标签");
        readTagPosition = 2;
        inputCageView.setVisibility(View.GONE);
        initTagViewOne.setVisibility(View.GONE);
        initTagViewTwo.setVisibility(View.VISIBLE);
        curTagView = initTagViewTwo;
        isReadTag = true;
        if (readTagEntityTwo == null)
            curTagView.initView("", "", curCage);
        else
            curTagView.initView(readTagEntityTwo.getTid(), readTagEntityTwo.getEpc(), curCage);
    }

    @Override
    public void initReadTag(ReadTagEntity readTagEntity) {

        if (readTagPosition == 1) {
            readTagEntityOne = readTagEntity;
        } else {
            readTagEntityTwo = readTagEntity;
        }
        curTagView.initView(readTagEntity.getTid(), readTagEntity.getEpc(), curCage);
    }

    private void setTitle(String title) {

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(title);
    }
}
